package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 订单管理
 */
@RestController
@RequestMapping("order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @PostMapping("submit")
    public R submit(@RequestBody Orders orders) {
        ordersService.submitOrders(orders);
        return R.success("");

    }

    /**
     * 后端分页
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public R<Page<Orders>> page(Integer page, Integer pageSize) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        ordersService.lambdaQuery()
                .orderByDesc(Orders::getCheckoutTime)
                .page(ordersPage);
        return R.success(ordersPage);
    }

    /**
     * 移动端-订单列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("userPage")
    public R<Page<OrdersDto>> userPage(Integer page, Integer pageSize) {
        //调用业务层查询订单信息（包括订单详情项）
        Page<OrdersDto> pageInfo = ordersService.userPage(page, pageSize);
        return R.success(pageInfo);
    }

    @PutMapping
    public R update(@RequestBody Orders orders) {
        ordersService.lambdaUpdate()
                .eq(Orders::getId, orders.getId())
                .set(Orders::getStatus, orders.getStatus()).update();
        return R.success("......");
    }
}


